﻿using Microsoft.JSInterop;
using MudBlazor;
using System.Collections.Concurrent;

namespace BlazorAppDemo.Common
{
    public class BrowserHelper
    {
        ConcurrentDictionary<string, Type> uiConfigSettingTypes = new ConcurrentDictionary<string, Type>();
        public ConcurrentDictionary<string, Type> UIConfigSettingTypes => uiConfigSettingTypes;
        public IJSRuntime JS { get; set; }
        public async void ToBack()
        {
            await JS.InvokeVoidAsync("history.back", Array.Empty<object?>());
        }

        /// <summary>
        /// 设置最新路由地址的名称：用于更换ID跳转的导航
        /// </summary>
        public Action<string> SetLastRouteTextAction;

        /// <summary>
        /// 跳转到登录页
        /// </summary>
        public Func<string, Task> ToLogin { get; set; }

        //Xs, Sm, 
        readonly List<Breakpoint> SmallBreakPoints = new List<Breakpoint> { Breakpoint.Sm, Breakpoint.Xs };

        //Md, Lg, Xl, Xxl,
        readonly List<Breakpoint> MiddleOrLargeBreakPoints = new List<Breakpoint> { Breakpoint.Md, Breakpoint.Lg, Breakpoint.Xl, Breakpoint.Xxl };

        /// <summary>
        /// 是否为小屏显示
        /// </summary>
        /// <returns></returns>
        public bool IsSmall(Breakpoint breakpoint)
        {
            return SmallBreakPoints.Any(p => breakpoint.ToString().StartsWith(p.ToString(), StringComparison.OrdinalIgnoreCase));
        }
        /// <summary>
        /// 是否为中屏或大屏显示
        /// </summary>
        /// <returns></returns>
        public bool IsLarge(Breakpoint breakpoint)
        {
            return MiddleOrLargeBreakPoints.Any(p => breakpoint.ToString().StartsWith(p.ToString(), StringComparison.OrdinalIgnoreCase));
        }
    }
}
